package com.hangwei.paperless.widget;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.hangwei.paperless.App;
import com.hangwei.paperless.BottomPopType;
import com.hangwei.paperless.R;
import com.hangwei.paperless.adapter.NATypeAdapter;
import com.hangwei.paperless.adapter.SealAdapter;
import com.hangwei.paperless.adapter.TypeAdapter;
import com.hangwei.paperless.model.bean.DictBean;
import com.hangwei.paperless.model.bean.UserInfo;
import com.hangwei.paperless.presenter.StaticPresenter;
import com.hangwei.paperless.utils.NotFastClickListener;
import com.twelve.all.net.CallBack;
import com.twelve.all.utils.LoadingDialog;
import com.twelve.all.utils.LogUtil;
import com.twelve.all.utils.SPKey;
import com.twelve.all.utils.SPUtil;
import com.twelve.all.utils.ToastUtil;
import com.twelve.all.utils.Tools;

import java.util.List;

/**
 * Created by 张龙臣 on 2020/10/12.
 */

public class MainBottomPopupWindow extends PopupWindow {

    private final Activity mActivity;

    public interface OnSealListener {
        void onSeal(String stampId);
    }

    public void setOnSealListener(OnSealListener onSealListener) {
        mOnSealListener = onSealListener;
    }

    private OnSealListener mOnSealListener;

    public interface OnNAListener {
        void onNA(String stampId, String naTypeId);
    }

    public void setNeedAlpha(boolean needAlpha) {
        this.needAlpha = needAlpha;
    }

    private boolean needAlpha = true;

    public void setOnNAListener(OnNAListener onNAListener) {
        mOnNAListener = onNAListener;
    }

    private OnNAListener mOnNAListener;

    public MainBottomPopupWindow(Activity context) {
        super(context);
        mActivity = context;
    }

    public void onSeal(String stampId) {
        if (mOnSealListener != null)
            mOnSealListener.onSeal(stampId);
    }

    public MainBottomPopupWindow(Activity context, View view, BottomPopType type) {
        super(context);
        // 下面是两种方法得到宽度和高度 getWindow().getDecorView().getWidth()
        mActivity = context;
        setContentView(view);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置popWindow弹出窗体可点击，这句话必须添加，并且是true
        setFocusable(true);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(Color.WHITE);
        setBackgroundDrawable(dw);
        // 设置popWindow的显示和消失动画
        setAnimationStyle(R.style.mypopwindow_anim_style);

        initView(view, type);

        //popWindow消失监听方法
        setOnDismissListener(new PopupWindow.OnDismissListener() {

            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
                lp.alpha = 1f;
                mActivity.getWindow().setAttributes(lp);
            }
        });
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        if (needAlpha) {
            WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
            lp.alpha = 0.7f;
            mActivity.getWindow().setAttributes(lp);
        }
        super.showAtLocation(parent, gravity, x, y);
    }

    private void initView(View view, BottomPopType type) {
        switch (type) {
            case NA:
                initNA(view);
                break;
            case Seal:
                initSeal(view);
                break;
            case EngDisp:
                view.findViewById(R.id.back).setOnClickListener(mNotFastClickListener);
                initEng(view);
                break;
            case Signature:
                break;
        }
    }

    private void initEng(View view) {
        MaxRecyclerView recyclerView = view.findViewById(R.id.recyclerView);
        MaxRecyclerView recyclerView1 = view.findViewById(R.id.recyclerView1);
        recyclerView.setLayoutManager(new GridLayoutManager(view.getContext(), 2));
        recyclerView1.setLayoutManager(new GridLayoutManager(view.getContext(), 2));
        TypeAdapter adapter = new TypeAdapter(null, 1);
        recyclerView.setAdapter(adapter);
        TypeAdapter adapter1 = new TypeAdapter(null, 1);
        recyclerView1.setAdapter(adapter1);
        LoadingDialog loadingDialog = new LoadingDialog(view.getContext());
        initEngData(adapter, 0, loadingDialog);
        initEngData(adapter1, 1, loadingDialog);
    }

    private void initEngData(TypeAdapter adapter, int code, LoadingDialog loadingDialog) {
        if (loadingDialog != null && !loadingDialog.isShowing())
            loadingDialog.show();
        String text = code == 0 ? "DiscrepancyType" : "DiscrepancyDescription";
        String userId = SPUtil.getString(SPKey.Account.USER_ID);
        StaticPresenter.getDictByIdOrCode(userId, null, text, 1, 100, new CallBack<List<DictBean>>() {
            @Override
            public void onCompleted(List<DictBean> dictBeans) {
                if (loadingDialog != null && loadingDialog.isShowing()) loadingDialog.dismiss();
                adapter.setNewData(dictBeans);
            }

            @Override
            public void onError() {
                if (loadingDialog != null && loadingDialog.isShowing()) loadingDialog.dismiss();
            }
        });
    }

    private void initNA(View view) {
        RelativeLayout rlChoose = view.findViewById(R.id.rlChoose);
        final TextView tvType = view.findViewById(R.id.tvType);
        RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
        LinearLayout llCancel = view.findViewById(R.id.llCancel);
        LinearLayout llConfirm = view.findViewById(R.id.llConfirm);
        final RecyclerView listView = view.findViewById(R.id.listView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mActivity);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(new SealAdapter());
        final String[] typeId = new String[1];
        llCancel.setOnClickListener(mNotFastClickListener);
        final NATypeAdapter adapter = new NATypeAdapter(null) {
            @Override
            protected void onAdapterItemClick(BaseViewHolder helper, DictBean item) {
                //弹框消失
                listView.setVisibility(View.GONE);
                tvType.setText(Tools.checkedString(item.code + ":" + item.name));
                typeId[0] = item.id;
            }
        };
        listView.setLayoutManager(new LinearLayoutManager(mActivity));
        listView.setAdapter(adapter);
        rlChoose.setOnClickListener(new NotFastClickListener() {
            @Override
            public void onClick(View v) {
                if (adapter.getData().size() > 0)
                    listView.setVisibility(View.VISIBLE);
            }
        });
        llConfirm.setOnClickListener(new NotFastClickListener() {
            @Override
            public void onClick(View v) {
               if (TextUtils.isEmpty( typeId[0])){
                   ToastUtil.showCenterToast("请选择NA类型！");
                   return;
               }
                if (mOnNAListener != null)
                    mOnNAListener.onNA("", typeId[0]);
//                if (App.stamps != null && App.stamps.size() > 0) {
//                    //有印章
//                    String stampId = null;
//                    for (UserInfo.StampBean stampBean : App.stamps) {
//                        if (stampBean.isChecked)
//                            stampId = stampBean.stampId;
//                    }
//                    if (TextUtils.isEmpty(stampId)) {
//                        ToastUtil.showCenterToast("请选择印章");
//                    } else {
//                        if (mOnNAListener != null)
//                            mOnNAListener.onNA(stampId, typeId[0]);
//                    }
//                } else {
//                    ToastUtil.showCenterToast("无印章");
//                }
            }
        });
        String userId = SPUtil.getString(SPKey.Account.USER_ID);
        StaticPresenter.getDictByIdOrCode(userId, null, "NAType", 1, 30, new CallBack<List<DictBean>>() {
            @Override
            public void onCompleted(List<DictBean> dictBeans) {
//                if (dictBeans != null && dictBeans.size() > 0) {
//                    dictBeans.get(0).checked = true;
//                    tvType.setText(Tools.checkedString(dictBeans.get(0).name));
//                    typeId[0] = dictBeans.get(0).id;
//                }
                LogUtil.i(dictBeans.toString());
                adapter.setNewData(dictBeans);
            }

            @Override
            public void onError() {

            }
        });
    }

    private void initSeal(View view) {
        RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
        LinearLayout llCancel = view.findViewById(R.id.llCancel);
        LinearLayout llConfirm = view.findViewById(R.id.llConfirm);
        llCancel.setOnClickListener(mNotFastClickListener);
        llConfirm.setOnClickListener(mNotFastClickListener);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mActivity);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(new SealAdapter());
    }

    private NotFastClickListener mNotFastClickListener = new NotFastClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.back:
                case R.id.llCancel:
                    MainBottomPopupWindow.this.dismiss();
                    break;
                case R.id.llConfirm:
                    String stampId = null;
                    for (UserInfo.StampBean stampBean : App.stamps) {
                        if (stampBean.isChecked)
                            stampId = stampBean.stampId;
                    }
                    if (TextUtils.isEmpty(stampId)) {
                        ToastUtil.showCenterToast("请选择印章");
                    } else {
                        if (mOnSealListener != null)
                            mOnSealListener.onSeal(stampId);
                    }
                    break;
            }
        }
    };
}
